<template>
    <div class='pwdLogin-wrapper'>
        <div class='login-tel'>
            <input type="text" v-model="userTel" placeholder="请输入手机号" pattern="[0-9]*">
        </div>
        <div class='login-tel'>
            <input type="text" v-model='userPwd' placeholder="请输入密码">
        </div>
        <div class="login-btn">
            <button @click="login">登录</button>
        </div>
        <div class='tab'>
            <span @click='goSmsLogin'>短信登录</span>
            <span @click='goRegistration'>快速注册</span>
        </div>
    </div>
</template>

<script>
import { Toast } from 'mint-ui';
import { LoginModel } from 'models/account.js';
import tools from 'utils/tools.js';
import { mapMutations } from 'vuex';

export default {
    name: 'SmsLoginWrapper',
    data() {
        return {
            userTel: '',
            userPwd: '',
            // 验证规则
            rules: {
                //手机号验证
                userTel: {
                    rule: /^1[23456789]\d{9}$/,
                    msg: '手机号不能为空，且为11位数字'
                },
                //密码验证
                userPwd: {
                    rule: /^\w{6,12}$/,
                    msg: '密码不能为空，且要6,12位'
                }
            }
        }
    },
    methods: {
        ...mapMutations(['userLogin']),
        //点击登录按钮
        login() {
            //前端验证
            if (!tools.validate('userTel',this.userTel)) return;
            if (!tools.validate('userPwd',this.userPwd)) return;
            //发送请求，后端验证
            const loginModel = new LoginModel();

            loginModel.login(this.userTel, this.userPwd).then(res => {
                const data = res.data;

                //提示信息
                Toast(data.msg);
                //登录失败
                if (!data.success) return;
                //登录成功 ==》跳转页面，存储用户信息
                this.userLogin(data.data);
                //跳转到我的页面中
                this.$router.push('/index');
            })
        },
        //快速注册
        goRegistration() {
            this.$router.push('/registration');
        },
        // 短信登录
        goSmsLogin() {
            this.$router.push('/');
        }
    }
}
</script>

<style scoped lang='scss'>
@import '~styles/mixins.scss';
@import '~styles/variables.scss';

.pwdLogin-wrapper {
    @include flex-column;
    margin-top: 2.8rem;
    width: 100%;
    font-size: .16rem;
    box-sizing: border-box;

    .login-tel {
        height: .66rem;
        width: 100%;
        padding: .1rem .15rem;
        box-sizing: border-box;

        input {
            width: 100%;
            height: 100%;
             border: 1px solid rgb(255, 170, 61);
            border-radius: .03rem;
            box-sizing: border-box;
            text-indent: .1rem;
        }
    }


    .login-btn {
        height: .66rem;
        width: 100%;
        padding: .1rem .15rem;
        box-sizing: border-box;


        button {
            width: 100%;
            height: 100%;
            text-align: center;
            color: #fff;
            background-color: rgb(255, 170, 61);
            border: 1px solid rgb(255, 170, 61);
            border-radius: .03rem;
            box-sizing: border-box;
        }
    }

    .tab {
        display: flex;
        justify-content: space-between;
        font-size: .14rem;
        padding: .05rem .15rem;
        box-sizing: border-box;
        color: rgb(255, 170, 61);
    }
}
</style>
